package com.boarsoft.boar.sequence.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.boar.sequence.biz.SequenceInfoBiz;
import com.boarsoft.boar.sequence.entity.SequenceInfo;
import com.boarsoft.boar.sequence.service.SequenceService;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;

@RestController
@RequestMapping("/info")
public class SequenceInfoAction {
	private static final Logger log = LoggerFactory.getLogger(SequenceInfoAction.class);

	@Autowired
	private SequenceService sequenceService;
	@Autowired
	private SequenceInfoBiz sequenceBiz;

	@RequestMapping("/save")
	@Authorized(code = "sequence.info.save")
	public ReplyInfo<Object> save(SequenceInfo o) {
		try {
			sequenceBiz.save(o);
			return ReplyInfo.SUCCESS;
		} catch (Exception e) {
			log.error("Error on save sequence {}", o, e);
			return ReplyInfo.FAILED;
		}
	}

	@RequestMapping("/delete")
	@Authorized(code = "sequence.info.delete")
	public ReplyInfo<Object> delete(String id) {
		if (Util.strIsEmpty(id)) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, "delete");
		}
		sequenceBiz.delete(id);
		return ReplyInfo.SUCCESS;
	}

	@RequestMapping("/list")
	@Authorized(code = "sequence.info.list")
	public ReplyInfo<Object> list(String catalog, String key, String orderBy, int pageNo, int pageSize) {
		PagedResult<SequenceInfo> pr = sequenceBiz.list(catalog, key, orderBy, pageNo, pageSize);
		return new ReplyInfo<Object>(true, JsonUtil.from(pr));
	}

	public SequenceInfoBiz getSequenceBiz() {
		return sequenceBiz;
	}

	public void setSequenceBiz(SequenceInfoBiz sequenceBiz) {
		this.sequenceBiz = sequenceBiz;
	}

	public SequenceService getSequenceService() {
		return sequenceService;
	}

	public void setSequenceService(SequenceService sequenceService) {
		this.sequenceService = sequenceService;
	}
}